package com.ws.project

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.net.wifi.WifiManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.ViewPropertyAnimator
import androidx.activity.viewModels
import androidx.core.animation.doOnEnd
import androidx.core.view.ViewPropertyAnimatorCompat
import androidx.core.view.marginEnd
import com.ws.project.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    var busState: BusState = BusState.PRECOMING
    var debug = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val model: Data by viewModels()
        model.bssid.observe(this){
            model.busId.value = getBusIdByBSSID(it,debug)
        }
        model.busId.observe(this){
            if(it!="Ошибка") {
                binding.textView.text=it
                when(busState) {
                    BusState.PRECOMING-> {
                        Thread {
                            busComing()
                            Thread.sleep(800)
                            BottomSheetFragment().show(supportFragmentManager, "BottomDialog")

                        }.start()
                    }
                    BusState.AWAY->{startPos()}
                    BusState.ONSTATION -> BottomSheetFragment().show(supportFragmentManager, "BottomDialog")
                }
            }
            else{
                when(busState){
                    BusState.ONSTATION->{busGoAway()}
                }
            }
        }

        setContentView(binding.root)
        runUpdater()
        binding.button1.setOnClickListener { debug = !debug }
        binding.button6.setOnClickListener { model.bssid.value = "02:00:00:00:00:00" }


    }
    fun runUpdater(){
        val model: Data by viewModels()
        Thread{
            while (true) {
                val wifiManager = getSystemService(WIFI_SERVICE) as WifiManager
                val mngr = wifiManager.connectionInfo
                if(model.bssid.value != mngr.bssid) {
                    model.bssid.postValue(mngr.bssid)
                }
                Thread.sleep(500)
            }
        }.start()
    }
    fun busGoAway(){
        ObjectAnimator.ofFloat(binding.imageView2,"x",-320f).apply {
            duration = 800
            start()
        }
        ObjectAnimator.ofFloat(binding.imageView2,"y",-2000f).apply {
            duration = 800
            start()
        }
        ObjectAnimator.ofFloat(binding.textView,"x",-320f).apply {
            duration = 800
            start()
        }
        ObjectAnimator.ofFloat(binding.textView,"y",-2000f).apply {
            duration = 800
            start()
        }.doOnEnd { startPos();busState = BusState.PRECOMING }
    }
    fun busComing(){
        binding.imageView2.animate().translationY(0f).duration = 800
        binding.textView.animate().translationY(0f).duration = 800
        busState = BusState.ONSTATION
    }
    fun startPos(){
        binding.imageView2.animate().x(0f).y(3000f).duration = 1
        binding.textView.animate().x(0f).y(3000f).duration = 1
        busState = BusState.AWAY
    }
}
enum class BusState{
    PRECOMING,
    ONSTATION,
    AWAY
}